package com.example.fastshare;

import com.example.fastshare.alljoyn.ApplicationService.HostChannelState;
import com.example.fastshare.alljoyn.MainApp;
import com.example.fastshare.alljoyn.Observer;
import com.example.fastshare.alljoyn.ApplicationService;
import com.example.fastshare.alljoyn.DialogBuilder;
import com.example.fastshare.alljoyn.Observable;
import com.example.fastshare.R;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class CreateChannelActivity extends FragmentActivity implements Observer {

	private EditText m_txtChannelName;
	private Button m_btnSetChannelName;
	private Button m_btnStartHostChannel;
	public static Button m_btnStopHostChannel;
	private Button m_btnQuit;
	private TextView m_txtHostNameShow;
	private TextView m_txtHostStateShow;
	private MainApp m_mainApp = null;

	private static final String TAG = "fastshare.host";
	private static final int HANDLE_APPLICATION_QUIT_EVENT = 0;
	private static final int HANDLE_CHANNEL_STATE_CHANGED_EVENT = 1;
	private static final int HANDLE_ALLJOYN_ERROR_EVENT = 2;
	public static final int DIALOG_SET_NAME_ID = 0;
	public static final int DIALOG_START_ID = 1;
	public static final int DIALOG_STOP_ID = 2;
	public static final int DIALOG_ALLJOYN_ERROR_ID = 3;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_create_channel);
		m_btnStartHostChannel = (Button) findViewById(R.id.activity_create_channel_StartHostChannel_Button);
		m_btnStartHostChannel.setOnClickListener(new OnClickListener() {

			@SuppressWarnings("deprecation")
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (m_mainApp.getM_HostChannelState() != HostChannelState.IDLE) {
					showDialog(DIALOG_STOP_ID);
					onCreateDialog(DIALOG_STOP_ID);
					m_btnStopHostChannel.setEnabled(false);
				} else {
					showDialog(DIALOG_SET_NAME_ID);
					onCreateDialog(DIALOG_SET_NAME_ID);
				}
				updateChannelState();

			}
		});
		m_btnStopHostChannel = (Button) findViewById(R.id.activity_create_channel_StopHostChannel_Button);
		m_btnStopHostChannel.setEnabled(false);
		m_btnStopHostChannel.setOnClickListener(new OnClickListener() {
			@SuppressWarnings("deprecation")
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showDialog(DIALOG_STOP_ID);
				onCreateDialog(DIALOG_STOP_ID);
				updateChannelState();
			}
		});
		m_btnQuit = (Button) findViewById(R.id.activity_create_channel_Back_Button);
		m_txtHostNameShow = (TextView) findViewById(R.id.activity_create_channel_HostNameShow_TextView);
		m_txtHostStateShow = (TextView) findViewById(R.id.activity_create_channel_HostStateShow_TextView);
		m_btnQuit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// m_mainApp.quit();
				// System.exit(1);
				onBackPressed();
			}
		});
		m_mainApp = (MainApp) getApplication();
		m_mainApp.chechIn();
		updateChannelState();
		m_mainApp.addObserver(this);
	}

	@Override
	public synchronized void update(Observable obs, Object obj) {
		String qualifier = (String) obj;

		if (qualifier.equals(MainApp.APPLICATION_QUIT_EVENT)) {
			Message message = m_handler
					.obtainMessage(HANDLE_APPLICATION_QUIT_EVENT);
			m_handler.sendMessage(message);
		}

		if (qualifier.equals(MainApp.HOST_CHANNEL_STATE_CHANGED_EVENT)) {
			Message message = m_handler
					.obtainMessage(HANDLE_CHANNEL_STATE_CHANGED_EVENT);
			m_handler.sendMessage(message);
		}

		if (qualifier.equals(MainApp.ALLJOYN_ERROR_EVENT)) {
			Message message = m_handler
					.obtainMessage(HANDLE_ALLJOYN_ERROR_EVENT);
			m_handler.sendMessage(message);
		}

	}

	private Handler m_handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case HANDLE_APPLICATION_QUIT_EVENT: {
				Log.i(TAG,
						"mHandler.handleMessage(): HANDLE_APPLICATION_QUIT_EVENT");

			}
				break;
			case HANDLE_CHANNEL_STATE_CHANGED_EVENT: {
				Log.i(TAG,
						"mHandler.handleMessage(): HANDLE_CHANNEL_STATE_CHANGED_EVENT");
				updateChannelState();
			}
				break;
			case HANDLE_ALLJOYN_ERROR_EVENT: {
				Log.i(TAG,
						"mHandler.handleMessage(): HANDLE_ALLJOYN_ERROR_EVENT");
			}
				break;
			default:
				break;
			}
		}
	};

	protected Dialog onCreateDialog(int id) {
		Log.i(TAG, "onCreateDialog()");
		Dialog result = null;
		switch (id) {
		case DIALOG_SET_NAME_ID: {
			DialogBuilder builder = new DialogBuilder();
			result = builder.createHostNameDialog(this, m_mainApp);
		}
			break;
		case DIALOG_START_ID: {
			DialogBuilder builder = new DialogBuilder();
			result = builder.createHostStartDialog(this, m_mainApp);
		}
			break;
		case DIALOG_STOP_ID: {
			DialogBuilder builder = new DialogBuilder();
			result = builder.createHostStopDialog(this, m_mainApp);
		}
			break;
		case DIALOG_ALLJOYN_ERROR_ID: {
			DialogBuilder builder = new DialogBuilder();
			result = builder.createAllJoynErrorDialog(this, m_mainApp);
		}
			break;
		}
		return result;
	}

	public synchronized void updateChannelState() {
		ApplicationService.HostChannelState channelState = m_mainApp
				.getM_HostChannelState();
		String hostName = m_mainApp.getM_HostChannelName();
		if (hostName == "") {
			hostName = "Not Set";
			m_btnStopHostChannel.setEnabled(false);
		}
		m_txtHostNameShow.setText(hostName);
		switch (channelState) {
		case IDLE:
			m_txtHostStateShow.setText("Idle");
			break;
		case NAMED:
			m_txtHostStateShow.setText("Named");
			m_btnStopHostChannel.setEnabled(true);
			break;
		case BOUND:
			m_txtHostStateShow.setText("Bound");
			m_btnStopHostChannel.setEnabled(true);
			break;
		case ADVERTISED:
			m_txtHostStateShow.setText("Advertised");
			m_btnStopHostChannel.setEnabled(true);
			break;
		case CONNECTED:
			m_txtHostStateShow.setText("Connected");
			m_btnStopHostChannel.setEnabled(true);
			break;
		default:
			m_txtHostStateShow.setText("Unknown");
			m_btnStopHostChannel.setEnabled(true);
			break;
		}
	}
}
